package com.settlement.system.controller;

import com.settlement.system.csv.CsvExportBuilder;
import com.settlement.system.csv.CsvExportUtil;
import com.settlement.system.common.enums.ExcelFileType;
import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.BasePager;
import com.settlement.system.model.entity.common.TblWpCumulativeIosSummary;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpCumulativeIosSummaryService;
import com.settlement.system.service.csv.DateSpiltCsvDatas;
import com.settlement.system.common.util.common.FasUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Map;

/**
 * AP_544累计收发存汇总表 前端控制器
 * @author Ximi
 * @since 2020-10-19
 */

@Api(tags = "AP_544累计收发存汇总表",description = "AP_544累计收发存汇总表")
@RestController
@RequestMapping("/wp/tblWpCumulativeIosSummary")
public class TblWpCumulativeIosSummaryController extends MergeApiExcelController<TblWpCumulativeIosSummary, Map<String, Object>> {

    @Value("${system.export-root.path}")
    private String exportRootPath;

    @Resource
    private ITblWpCumulativeIosSummaryService tblWpCumulativeIosSummaryService;

    @Override
    public IBaseService init() {
        return tblWpCumulativeIosSummaryService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblWpCumulativeIosSummary.class;
    }


    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        TblWpCumulativeIosSummary foot=tblWpCumulativeIosSummaryService.getFoot(model);
        result.put("foot", foot==null? getModelClass():foot);
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model,"vendorCode");
        super.setParamsModel(model);
    }

    @Override
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> params = buildParam(request);
        setParamsModel(params);
        Object fileName = "WP-累计收发存明细";
        String path = exportRootPath + System.currentTimeMillis()  + File.separator;
        CsvExportUtil.createCsv()
                .headClass(getModelClass())
                .fileName(fileName.toString())
                .model(CsvExportBuilder.MULTI_ZIP_MODEL)
                .parentPath(path)
                .fileType(ExcelFileType.XLSX)
                .csvDatas(new DateSpiltCsvDatas(tblWpCumulativeIosSummaryService, params))
                .export(request,response);
    }

    private String verifyRequest(Map<String, Object> params) {
        String startingMonth = params.get("startingMonth") != null ? params.get("startingMonth").toString() : "";
        String closingMonth = params.get("closingMonth") != null ? params.get("closingMonth").toString() : "";

        if (!StringUtils.isEmpty(startingMonth)
                && startingMonth.compareTo(closingMonth) > 0) {
            return "起始月份不能大于截止月份";
        }

        return null;
    }

    @Override
    @ApiOperation("分页查询")
    @RequestMapping(value = "list", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse getPageList(HttpServletRequest request, @RequestBody BasePager<Map<String, Object>> model) throws ServiceException {
        String verification = verifyRequest(model.getModel());
        if (!StringUtils.isEmpty(verification)) {
            return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), verification);
        }
        return super.getPageList(request,model);
    }
}

